﻿using Microsoft.Extensions.Configuration;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Commonality.RedisHelper.Init
{
    /// <summary>
    /// Redis管理中心   创建Redis链接
    /// </summary>
    public class RedisManager
    {
        /// <summary>
        /// redis配置文件信息
        /// </summary>
        private static RedisConfigInfo RedisConfigInfo = new RedisConfigInfo();

        /// <summary>
        /// Redis客户端池化管理
        /// </summary>
        private static PooledRedisClientManager prcManager;

        /// <summary>
        /// 静态构造方法，初始化链接池管理对象
        /// </summary>
        static RedisManager()
        {
            CreateManager();
        }


        /// <summary>
        /// 创建链接池管理对象
        /// </summary>
        private static void CreateManager()
        {
            //读取配置文件
            var builder = new ConfigurationBuilder()
                      .SetBasePath(Directory.GetCurrentDirectory())
                      .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true).Build().GetSection("RedisConfigInfo")
                                                     .Get<RedisConfigInfo>();

            string[] WriteServerConStr = builder.WriteServerList.Split(',');
            string[] ReadServerConStr = builder.ReadServerList.Split(',');

            //List < RedisEndpoint> readWriteHosts = new List<RedisEndpoint>();
            //foreach (var item in WriteServerConStr)
            //{
            //    RedisEndpoint redisEndpoint = new RedisEndpoint();
            //    redisEndpoint.Host = item.Split(':')[0];
            //        redisEndpoint.Port = int.Parse(item.Split(':')[1]);
            //       redisEndpoint.Password = RedisConfigInfo.password;
            //    readWriteHosts.Add(redisEndpoint);
            //}
            //List<RedisEndpoint> readOnlyHosts = new List<RedisEndpoint>();
            //foreach (var item in ReadServerConStr)
            //{
            //    RedisEndpoint redisEndpoint = new RedisEndpoint();
            //    redisEndpoint.Host = item.Split(':')[0];
            //    redisEndpoint.Port = int.Parse(item.Split(':')[1]);
            //    redisEndpoint.Password = RedisConfigInfo.password;
            //    readOnlyHosts.Add(redisEndpoint);
            //}
            // 设置 Redis 连接信息，


            //看源码源码牛逼，源码使用了原子操作，保证了线程安全。
            ///提供redis客户端连接的线程安全池。
            ///允许主写和读复制主机的负载平衡，理想的
            /// 1个主副本和多个读副本。注意设置密码的方式是通过Host@分割 elg：123456@192.168.。。。:6379
            prcManager = new PooledRedisClientManager(ReadServerConStr, WriteServerConStr,
                          new RedisClientManagerConfig
                          {
                              MaxWritePoolSize = RedisConfigInfo.MaxWritePoolSize,
                              MaxReadPoolSize = RedisConfigInfo.MaxReadPoolSize,
                              AutoStart = RedisConfigInfo.AutoStart,
                          });
            //注意这种方式没有用池
            //提供redis客户端的线程安全检索，因为每个客户端都是一个新的。
            //允许配置不同的读写和只读主机
             //prcManager = new BasicRedisClientManager(readWriteHosts, readOnlyHosts);
        }

        /// <summary>
        /// 客户端缓存操作对象
        /// </summary>
        public static IRedisClient GetClient()
        {

          var client=   prcManager.GetClient();
            
            return client;
        }
    }
}
